package com.csun.nusing.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.DeviceSleepbyJsonDateJsonBean;
import com.csun.nusing.bean.jsonbean.DeviceSleepbyJsonDateJsonBean.DeviceSleepDataBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能床垫数据界面 ClassName: BedDetailActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-26
 */
public class BedDetailActivity extends BaseActivity<CommonPresenter> implements
		IBase {

	private TextView inbedstate, heartbeat, blood, huxi, fanshen;
	private ImageView bedback;
	private Bundle isFamilyFlag;
	private String deviceId;
	private String authorization;
	protected DeviceSleepbyJsonDateJsonBean devicesearchsleepJsonBean;
	private DeviceSleepDataBean sleepdate;

	@Override
	public int getContentViewId() {
		return R.layout.activity_bed_detail;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	/**
	 * 初始化控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	private void findView() {
		inbedstate = findViewById(R.id.bed_tv_sport);
		heartbeat = findViewById(R.id.bed_tv_heartbeat);
		blood = findViewById(R.id.bed_tv_blood);
		huxi = findViewById(R.id.bed_huxi_blood);
		fanshen = findViewById(R.id.bed_fanshen_blood);
		bedback = findViewById(R.id.bed_back);
		bedback.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

	}

	@Override
	public void initData() {
		super.initData();

		isFamilyFlag = getIntent().getBundleExtra("isFamily");
		deviceId = getIntent().getStringExtra("deviceId");

		/**
		 * 请求数据
		 */
		authorization = (String) SPUtils.get(this, "authorization", "");

		Map<String, String> param = new HashMap<String, String>();
		param.put("id", "" + deviceId);
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "------"+result);
							Gson gson = new Gson();
							devicesearchsleepJsonBean = gson.fromJson(result,
									DeviceSleepbyJsonDateJsonBean.class);
							if (devicesearchsleepJsonBean != null
									&& devicesearchsleepJsonBean.getData() != null) {
								controlData(devicesearchsleepJsonBean.getData());
							}
						}
					}
				}).start(UrlBean.SEARCHSLEEPDATEBUIDURL);
	}

	protected void controlData(List<DeviceSleepDataBean> data) {
		if (data.size() != 0) {
			sleepdate = data.get(0);
			if (sleepdate.getIsOnBed() == 1) {
				inbedstate.setText(R.string.in_bed);
			} else {
				inbedstate.setText(R.string.bed_detail_out_bed_state);
			}
			heartbeat.setText(""+sleepdate.getHeartRate());
			blood.setText(""+sleepdate.getBreathe());
			if (sleepdate.getTurn() == 0) {
				huxi.setText(R.string.huxi_alarm);
			} else {
				huxi.setText(R.string.bed_detail_none);
			}
			//翻身报警
			if (sleepdate.getTurn() == 0) {
				fanshen.setText(R.string.fan_shen_remind);
			} else {
				fanshen.setText(R.string.bed_detail_none);
			}
		}
	}

}
